package com.mt.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import com.mt.system.api.service.IMenuService;
import com.mt.system.entity.MenuEntity;
import com.mt.system.service.model.MenuServiceModel;
import org.springframework.stereotype.Service;

@Service
public class MenuServiceImpl implements IMenuService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(MenuServiceImpl.class);

    @Autowired
    private MenuServiceModel menuServiceModel;

    public ServiceResult<MenuEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<MenuEntity> result = null;
        try {
            result = menuServiceModel.selectInfo(params);
        } catch (Exception e) {
            LOGGER.error("[MenuServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<MenuEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<MenuEntity>> result = new ServiceResult<List<MenuEntity>>();
        List<MenuEntity> rootMenuList = null;
        try {
            result = menuServiceModel.selectList(params, pagerInfo);
            if (result.getSuccess() && result.getResult() != null && result.getResult().size() > 0) {
                List<MenuEntity> menuList = result.getResult();
                rootMenuList = new ArrayList<MenuEntity>();
                for (MenuEntity entity : menuList) {
                    if (entity.getParentMenuId() == 0) {
                        rootMenuList.add(entity);
                    }
                }
                menuList.removeAll(rootMenuList);
                for (MenuEntity entity : rootMenuList) {
                    loopMenu(entity, menuList);;
                }
            }
            result.setResult(rootMenuList);
        } catch (Exception e) {
            LOGGER.error("[MenuServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    private void loopMenu(MenuEntity menuEntity, List<MenuEntity> menuList) {
        for (MenuEntity entity : menuList) {
            if (entity.getParentMenuId().equals(menuEntity.getMenuId())) {
                List<MenuEntity> childMenuList = menuEntity.getChildMenuList();
                if (childMenuList == null) {
                    childMenuList = new ArrayList<MenuEntity>();
                    menuEntity.setChildMenuList(childMenuList);
                }
                childMenuList.add(entity);
                loopMenu(entity, menuList);
            }
        }
    }

    public ServiceResult<Integer> doInsert(MenuEntity menuEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = menuServiceModel.insert(menuEntity);
        } catch (Exception e) {
            LOGGER.error("[MenuServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer menuId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = menuServiceModel.deleteByPrimaryKey(menuId);
        } catch (Exception e) {
            LOGGER.error("[MenuServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(MenuEntity menuEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = menuServiceModel.updateByPrimaryKeySelective(menuEntity);
        } catch (Exception e) {
            LOGGER.error("[MenuServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

}
